/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ventasautomotrices;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 *
 * @author Chapas
 */
public class AgregarEmpleado extends javax.swing.JFrame {

    JOptionPane a = new JOptionPane();
    private javax.swing.JDialog dlgMensajes = new javax.swing.JDialog();
    public javax.swing.JLabel lblMensajeDialogo = new javax.swing.JLabel();
    javax.swing.JButton btnOkMensajes;
    /**
     * Creates new form AgregarEmpleado
     */
    ControlDatos cdatos = new ControlDatos ();
    public AgregarEmpleado() {
        
         dlgMensajes = new javax.swing.JDialog();
        lblMensajeDialogo = new javax.swing.JLabel();
        btnOkMensajes = new javax.swing.JButton();
        
         dlgMensajes.setBackground(new java.awt.Color(204, 204, 204));

        btnOkMensajes.setText("OK");
        btnOkMensajes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkMensajesActionPerformed(evt);
            }
        });
        btnOkMensajes.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnOkMensajesKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout dlgMensajesLayout = new javax.swing.GroupLayout(dlgMensajes.getContentPane());
        dlgMensajes.getContentPane().setLayout(dlgMensajesLayout);
        dlgMensajesLayout.setHorizontalGroup(
            dlgMensajesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dlgMensajesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblMensajeDialogo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(dlgMensajesLayout.createSequentialGroup()
                .addGap(113, 113, 113)
                .addComponent(btnOkMensajes)
                .addContainerGap(136, Short.MAX_VALUE))
        );
        dlgMensajesLayout.setVerticalGroup(
            dlgMensajesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dlgMensajesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblMensajeDialogo, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnOkMensajes)
                .addContainerGap())
        );
          initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        ctipo = new javax.swing.JComboBox();
        tcelular = new javax.swing.JTextField();
        tdireccion = new javax.swing.JTextField();
        tapellido = new javax.swing.JTextField();
        tnombre = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        tpass = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTextArea1.setEditable(false);
        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setText("Ingrese todos los datos \ndel formulario presente.\n\nTodos los datos son reque-\nridos, no deje ninguno\nen blanco.\n\n");
        jScrollPane1.setViewportView(jTextArea1);

        jLabel1.setText("Nombre:");

        jLabel2.setText("Apellido:");

        jLabel3.setText("Dirección:");

        jLabel4.setText("Celular:");

        jLabel5.setText("Tipo:");

        ctipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Gerente", "Vendedor" }));

        tapellido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tapellidoActionPerformed(evt);
            }
        });

        tnombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tnombreActionPerformed(evt);
            }
        });

        jLabel6.setText("Datos de Usuario");

        jLabel8.setText("Password:");

        jButton1.setText("Insertar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel9.setText("Datos de Empleado");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1))
                                .addGap(28, 28, 28)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(ctipo, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(tnombre, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
                                        .addComponent(tapellido)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel6)
                                            .addComponent(tcelular, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addComponent(tdireccion, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addGap(18, 18, 18)
                                .addComponent(tpass, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 254, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(210, 210, 210)
                                .addComponent(jButton1))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(134, 134, 134)
                                .addComponent(jLabel9)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9)
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(tnombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(tapellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(tdireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(tcelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(ctipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(28, 28, 28)
                        .addComponent(jLabel6)
                        .addGap(48, 48, 48)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(tpass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(46, 46, 46))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
         
        if(this.validateEntries()){
            if (this.insertarEmp()){
                
            }       
        }             // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void tapellidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tapellidoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tapellidoActionPerformed

    private void tnombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tnombreActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tnombreActionPerformed

    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ctipo;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField tapellido;
    private javax.swing.JTextField tcelular;
    private javax.swing.JTextField tdireccion;
    private javax.swing.JTextField tnombre;
    private javax.swing.JPasswordField tpass;
    // End of variables declaration//GEN-END:variables


    public boolean insertarEmp (){
    
        boolean insertado = false;
        String nombre = this.getTnombre().getText();
        String apellido = this.getTapellido().getText();
        String direccion = this.getTdireccion().getText();
        String celular = this.getTcelular().getText();
        int tipo = this.getCtipo().getSelectedIndex();
        try {
            insertado = cdatos.insertarEmpleado(nombre, apellido, direccion, celular, tipo);
            this.insertarUser();
             //JOptionPane.showMessageDialog(this, "Insersión completa");
             mostrarMensaje("Insersión completa.", "Exito", 200, 100);
        } catch (SQLException ex) {
            Logger.getLogger(AgregarEmpleado.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(AgregarEmpleado.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(AgregarEmpleado.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AgregarEmpleado.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (!insertado){
            //JOptionPane.showMessageDialog(this, "Error inesperado al ingrear empleado");
            mostrarMensaje("Error inesperado al ingrear empleado", "Error", 200, 100);
        }
        
        return insertado;
    
    }
    
    public boolean insertarUser(){
        
        boolean insertado = false;
                
                String pass = this.getTpass().getPassword().toString();
                
                insertado = cdatos.insertarUser(pass, this.getTnombre().getText(), this.getTapellido().getText());
                
        return insertado;
    }

    public boolean validateEntries(){
        boolean test = true;
        
        if(this.getTnombre().getText().equalsIgnoreCase("")||this.getTnombre().getText().isEmpty()||getTnombre().getText().length()<2){
            test = false;
           //a.showMessageDialog(this, "Error el campo nombre es incorrecto, es probale que esté vacío.");
           mostrarMensaje("Error el campo nombre es incorrecto, es probale que esté vacío.", "Error", 400, 100);
        } else if (this.getTapellido().getText().equalsIgnoreCase("")||this.getTapellido().getText().isEmpty()||getTapellido().getText().length()<2){
            test = false;
           //a.showMessageDialog(this, "Error el campo apellido es incorrecto, es probale que esté vacío. ");
           mostrarMensaje("Error el campo apellido es incorrecto, es probale que esté vacío.", "Error", 400, 100);
        } else if (this.getTdireccion().getText().equalsIgnoreCase("")||this.getTdireccion().getText().isEmpty()||getTdireccion().getText().length()<2){
            test = false;
            //a.showMessageDialog(this, "Error el campo dirección es incorrecto, es probale que esté vacío.");
            mostrarMensaje("Error el campo dirección es incorrecto, es probale que esté vacío.", "Error", 400, 100);
        } else if (this.getTcelular().getText().equalsIgnoreCase("")||this.getTcelular().getText().isEmpty()||getTcelular().getText().length()<2){
            test = false;
            //a.showMessageDialog(this, "Error el campo celular es incorrecto, es probale que esté vacío.");
            mostrarMensaje("Error el campo celular es incorrecto, es probale que esté vacío.", "Error", 400, 100);
        } 
        
        return test;
    }

    /**
     * @return the ctipo
     */
    public javax.swing.JComboBox getCtipo() {
        return ctipo;
    }

    /**
     * @return the jTextArea1
     */
    public javax.swing.JTextArea getjTextArea1() {
        return jTextArea1;
    }

    /**
     * @param jTextArea1 the jTextArea1 to set
     */
    public void setjTextArea1(javax.swing.JTextArea jTextArea1) {
        this.jTextArea1 = jTextArea1;
    }

    /**
     * @return the tapellido
     */
    public javax.swing.JTextField getTapellido() {
        return tapellido;
    }

    /**
     * @param tapellido the tapellido to set
     */
    public void setTapellido(String tapellido) {
        this.tapellido.setText(tapellido);
    }

    /**
     * @return the tcelular
     */
    public javax.swing.JTextField getTcelular() {
        return tcelular;
    }

    /**
     * @param tcelular the tcelular to set
     */
    public void setTcelular(String tcelular) {
        this.tcelular.setText(tcelular);
    }

    /**
     * @return the tdireccion
     */
    public javax.swing.JTextField getTdireccion() {
        return tdireccion;
    }

    /**
     * @param tdireccion the tdireccion to set
     */
    public void setTdireccion(String tdireccion) {
        this.tdireccion.setText(tdireccion); 
    }

    /**
     * @return the tnombre
     */
    public javax.swing.JTextField getTnombre() {
        return tnombre;
    }

    /**
     * @param tnombre the tnombre to set
     */
    public void setTnombre(String tnombre) {
        this.tnombre.setText(tnombre);
    }

    /**
     * @return the tpass
     */
    public javax.swing.JPasswordField getTpass() {
        return tpass;
    }

    /**
     * @param tpass the tpass to set
     */
    public void setTpass(String tpass) {
        this.tpass.setText(tpass);
    }
    
    public void setCtipo(int i) {
        this.ctipo.setSelectedIndex(i);
    }

    public JOptionPane getA() {
        return this.a;
    }
    
    public void mostrarMensaje( String mensaje, String titulo, int ancho, int alto ){
        lblMensajeDialogo.setText(mensaje);
        dlgMensajes.setTitle(titulo);
        dlgMensajes.setBounds(0, 0, ancho, alto);
        dlgMensajes.setSize(ancho, alto);
        dlgMensajes.setLocationRelativeTo(null);
        dlgMensajes.setVisible(true);
        btnOkMensajes.grabFocus();
    }
    private void btnOkMensajesActionPerformed(java.awt.event.ActionEvent evt) {                                              
        dlgMensajes.setVisible(false);
    }  
    
        private void btnOkMensajesKeyPressed(java.awt.event.KeyEvent evt) {                                         
        if(evt.getKeyCode()==10 || evt.getKeyCode()==32){
            btnOkMensajesActionPerformed(null);
        }
    }

}
